﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Business.Proj;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageAssembleTaskList : BaseForm.frmBaseMdiChildForm
    {
        #region vars
        private ProdPlanOrderItemEntity mSelectedPlanOrderItem;
        #endregion

        #region ctors
        public frmManageAssembleTaskList()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindCombobo()
        {
            DataTable dt = ProdTechnicBs.GetProdTechnicAll().Tables[0];
            cmbTechnic.DataSource = dt;
            cmbTechnic.ValueMember = "TECHNIC_ID";
            cmbTechnic.DisplayMember = "TECHNIC_NAME";

            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "POI", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "PDTYPE", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit2, "POI", false);
        }

        private void SetField()
        {
            ProdTechnicEntity m= ProdTechnicBs.GetProdTechnicByTechnicCode("Z");
            cmbTechnic.SetSelectedValue(m.TechnicId);
            cmbStatus.SetSelectedValue("POI03");
        }

        private string GetCondition()
        {
            string Condition = "";
            if (txtWorkingGroup.Tag != null)
            {
                Condition += " AND GROUP_ID='" + ((MstWorkingGroupEntity)txtWorkingGroup.Tag).GroupId + "' ";
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                Condition += " AND DELIVERY_DATE>='" + ((DateTime)dtpDeliveryDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00' ";
            }
            if (dtpDeliveryDateTo.EditValue!=null)
            {
                Condition += " AND DELIVERY_DATE<='" + ((DateTime)dtpDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            if (cmbTechnic.SelectedValue != null && string.IsNullOrEmpty(cmbTechnic.SelectedValue.ToString()) == false)
            {
                Condition += " AND TECHNIC_ID='" + cmbTechnic.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(txtPlanbOrderItemCode.Text) == false)
            {
                Condition += " AND ORDER_ITEM_CODE LIKE '%" + txtPlanbOrderItemCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProdPlanOrderCode.Text) == false)
            {
                Condition += " AND PLAN_ORDER_CODE LIKE '%" + txtProdPlanOrderCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            {
                Condition += " AND CONTRACT_MODEL LIKE '%"+txtContractModel.Text+"%' ";
            }
            if (cmbStatus.SelectedValue != null && string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString())==false)
            {
                Condition += " AND STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            else
            {
                Condition += " AND STATUS NOT IN ('POI07','POI08','POI09','POI10','POI11')";
            }
            return Condition;
        }

        private void BindGridMain()
        {
            string Condition = GetCondition();
            DataTable dt = ProdProductTechnicBs.GetDtWorkingGroupList(Condition,txtRowCount.Text);
            gvMain.DataSource = dt;
            DataColumn dtCol = new DataColumn();
            dtCol.ColumnName = "IS_PICKING";
            dtCol.DataType = System.Type.GetType("System.Boolean");
            dtCol.DefaultValue = false;
            dt.Columns.Add(dtCol);
            foreach (DataRow dr in dt.Rows)
            {
                mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(dr["PLAN_ORDER_ITEM_ID"].ToString());
                if (mSelectedPlanOrderItem != null)
                {
                    DataTable dtDetail = ProdPlanOrderItemBs.GetAssembleTaskList(mSelectedPlanOrderItem.PlanOrderItemId);
                    bool bFlag = false;
                    foreach (DataRow drDetail in dtDetail.Rows)
                    {
                        if (Convert.ToDecimal(drDetail["PLAN_QTY"].ToString()) != Convert.ToDecimal(drDetail["STORAGE_TOTAL_QTY"].ToString()))
                        {
                            bFlag = false;
                            break;
                        }
                        else
                        {
                            bFlag = true;
                        }
                    }
                    if (bFlag)
                    {
                        dr["IS_PICKING"] = true;
                    }
                }
            }
        }

        private void BindGridDetail()
        {
            if (mSelectedPlanOrderItem == null)
            {
                return;
            }
            ProdPlanOrderEntity prodPlanOrder = ProdPlanOrderBs.GetProdPlanOrderByID(mSelectedPlanOrderItem.PlanOrderId);
            if (prodPlanOrder==null)
            {
                return;
            }
            DataTable dt = ProdPlanOrderItemBs.GetAssembleTaskList(mSelectedPlanOrderItem.BomId, prodPlanOrder.PlanOrderId);
            dt.Columns.Add("PLAN_QTY", typeof(decimal));
            foreach (DataRow dr2 in dt.Rows)
            {
                dr2["PLAN_QTY"] = ((decimal)dr2["UNIT_DOSE"]) * prodPlanOrder.Qty;
            }
            gvDetail.DataSource = dt;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobo();
            SetField();
            BindGridMain();
        }

        private void txtWorkingGroup_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                frmMaintainWorkingGroupQuery frm = new frmMaintainWorkingGroupQuery();
                if (frm.ShowDialog() == DialogResult.OK && frm.SelectedMstWorkingGroupPerson != null)
                {
                    MstWorkingGroupEntity m = MstWorkingGroupBs.GetMstWorkingGroupByID(frm.SelectedMstWorkingGroupPerson.GroupId);
                    txtWorkingGroup.Text = m.GroupName;
                    txtWorkingGroup.Tag = m;
                }
                else
                {
                    txtWorkingGroup.Text = "";
                    txtWorkingGroup.Tag = null;
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindGridMain();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (e.Column.FieldName.Equals("PRODUCT_CODE"))
            //{
            //    string sProductCode = e.CellValue.ToString();
            //    try
            //    {
            //        EsolWevServices.EsolService ws = new EsolWevServices.EsolService();
            //        byte[] arrFileData = ws.DownloadFile(sProductCode + ".jpg");
            //        Image img = CommonFunctions.byteArrayToImage(arrFileData);
            //        frmProdBlueprintViewer frm = new frmProdBlueprintViewer(img);
            //        frm.ShowDialog();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("图片不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //}
        }

        private void gridView2_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (e.Column.FieldName.Equals("PRODUCT_CODE"))
            //{
            //    string sProductCode = e.CellValue.ToString();
            //    try
            //    {
            //        EsolWevServices.EsolService ws = new EsolWevServices.EsolService();
            //        byte[] arrFileData = ws.DownloadFile(sProductCode + ".jpg");
            //        Image img = CommonFunctions.byteArrayToImage(arrFileData);
            //        frmProdBlueprintViewer frm = new frmProdBlueprintViewer(img);
            //        frm.ShowDialog();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("图片不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //}
            //else if (e.Column.FieldName.Equals("ORDER_ITEM_CODE"))
            //{
            //    if (e.CellValue == null)
            //    {
            //        return;
            //    }
            //    ProdPlanOrderItemEntity prodPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByCode(e.CellValue.ToString());
            //    frmProdWorkingReport frm = new frmProdWorkingReport(prodPlanOrderItem);
            //    if (frm.ShowDialog() == DialogResult.OK)
            //    {
            //        BindGridDetail();
            //    }
            //}
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            ProdPlanOrderItemEntity mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(dr["PLAN_ORDER_ITEM_ID"].ToString());
            frmMaintainAssemblePicking frm = new frmMaintainAssemblePicking(mSelectedPlanOrderItem);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
                gvDetail.DataSource = null;
            }
        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if ((bool)gridView1.GetDataRow(e.RowHandle)["IS_PICKING"])
            {
                e.Appearance.BackColor = Color.LawnGreen;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            if (dr == null)
                return;
            mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(dr["PLAN_ORDER_ITEM_ID"].ToString());
            BindGridDetail();
        }

        private void tsmPrint_Click(object sender, EventArgs e)
        {
            //if (mSelectedPlanOrderItem == null)
            //{
            //    MessageBox.Show("请选择要打印的生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //ProdPlanOrderEntity mProdPlanOrder = ProdPlanOrderBs.GetProdPlanOrderByID(mSelectedPlanOrderItem.PlanOrderId);
            //frmCertificatePrint frm = new frmCertificatePrint(mProdPlanOrder);
            //frm.ShowDialog();
        }

        #endregion

    }
}
